package com.qianchang.microfinance.view.yfbpay;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.YBCheckBankCardBean;

import java.util.ArrayList;
import java.util.List;

public class BankCardRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{

    private Context context;
    private int CARDITEM_TYPE = 1;
    private int ADDCARD_TYPE = 2;
    private List<YBCheckBankCardBean> listBankCard = new ArrayList<YBCheckBankCardBean>();
    private OnItemClickListener mOnItemClickListener = null;
    private OnButClickListener onButClickListener = null;


    public BankCardRvAdapter(Context context, List<YBCheckBankCardBean> mlistBankCard){
        this.context = context;
        this.listBankCard = mlistBankCard;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);

        if(viewType == CARDITEM_TYPE){
            View itemView = layoutInflater.inflate(R.layout.item_locrvcard_layout,parent,false);
            itemView.setOnClickListener(this);
            CardItemViewHolder cardItemViewHolder = new CardItemViewHolder(itemView);
            return cardItemViewHolder;
        }
        if(viewType == ADDCARD_TYPE){
            View addCardView = layoutInflater.inflate(R.layout.item_addcard_layout,parent,false);
            addCardView.setOnClickListener(this);
            AddCardViewHolder addCardViewHolder = new AddCardViewHolder(addCardView);
            return addCardViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof CardItemViewHolder){
            YBCheckBankCardBean ybCheckBankCardBean = listBankCard.get(position);
//            ((CardItemViewHolder) holder).tvCarName.setText( ybCheckBankCardBean.getBankcode() + "(" + ybCheckBankCardBean.getCardname() +  ")");
            ((CardItemViewHolder) holder).tvCarName.setText( ybCheckBankCardBean.getBankcode());
            ((CardItemViewHolder) holder).tvCarNub.setText("尾号" + "(" + ybCheckBankCardBean.getCardlast() +  ")");


            if(ybCheckBankCardBean.getIsAuth().equals("1")){            //1为默认支付卡 0不是
                ((CardItemViewHolder) holder).tvDefut.setText("已设为默认卡");
                ((CardItemViewHolder) holder).tvDefut.setTextColor((context.getResources().getColor(R.color.color_fe5b00)));
                ((CardItemViewHolder) holder).tvDefut.setBackground(context.getResources().getDrawable(R.drawable.rect_rounded_blueborder_shape));
            }else {
                ((CardItemViewHolder) holder).tvDefut.setText("设置为默认卡");
                ((CardItemViewHolder) holder).tvDefut.setTextColor((context.getResources().getColor(R.color.common_black_color_9)));
                ((CardItemViewHolder) holder).tvDefut.setBackground(context.getResources().getDrawable(R.drawable.rect_rounded_grayborder_shape));
            }

            ((CardItemViewHolder) holder).tvDefut.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onButClickListener != null){
                        onButClickListener.onButClick(view,position);
                    }
                }
            });

            holder.itemView.setTag(position);
        }

        if(holder instanceof AddCardViewHolder){
            holder.itemView.setTag(position);
        }

    }

    @Override
    public int getItemCount() {
        if(listBankCard != null && listBankCard.size() > 0){
            return listBankCard.size() + 1;
        }
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == (listBankCard.size())){
            return ADDCARD_TYPE;
        }else {
            return CARDITEM_TYPE;
        }
    }

    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null){
            mOnItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }

    public void setData(List<YBCheckBankCardBean> mListDat){
        if(listBankCard != null && mListDat != null){
            listBankCard = mListDat;
        }else {
            listBankCard.clear();
        }
        notifyDataSetChanged();
    }



    public class CardItemViewHolder extends RecyclerView.ViewHolder {

        public TextView tvCarName;
        public TextView tvCarNub;
        public TextView tvDefut;



        public CardItemViewHolder(View itemView) {
            super(itemView);
            tvCarName = (TextView) itemView.findViewById(R.id.tv_name_id);
            tvCarNub = (TextView)itemView.findViewById(R.id.tv_carNub_id);
            tvDefut = (TextView) itemView.findViewById(R.id.tv_setdefut_id);
        }


    }

    public class AddCardViewHolder extends RecyclerView.ViewHolder{


        public AddCardViewHolder(View itemView) {
            super(itemView);
        }
    }



    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnButClickListener {
        void onButClick(View view , int position);
    }


    public void setOnButClickListener(OnButClickListener listener) {
        this.onButClickListener = listener;
    }

}
